using System;
using System.Xml;
using System.Globalization;

namespace Pitoshka.Tools.Helpers
{
	public class XmlHelper
	{
		public static void SetAttribute(XmlNode node, string name, string value)
		{
			XmlAttribute attr = node.Attributes[name];
			if (attr == null)
			{
				attr = node.OwnerDocument.CreateAttribute(name);
				node.Attributes.Append(attr);
			}
			attr.Value = value;
		}
		public static void SetAttribute(XmlNode node, string name, int value)
		{
			XmlAttribute attr = node.Attributes[name];
			if (attr == null)
			{
				attr = node.OwnerDocument.CreateAttribute(name);
				node.Attributes.Append(attr);
			}
			attr.Value = value.ToString();
		}
		public static void SetAttribute(XmlNode node, string name, double value)
		{
			XmlAttribute attr = node.Attributes[name];
			if (attr == null)
			{
				attr = node.OwnerDocument.CreateAttribute(name);
				node.Attributes.Append(attr);
			}
			attr.Value = value.ToString();
		}
		public static void SetAttribute(XmlNode node, string name, DateTime value)
		{
			XmlAttribute attr = node.Attributes[name];
			if (attr == null)
			{
				attr = node.OwnerDocument.CreateAttribute(name);
				node.Attributes.Append(attr);
			}
			attr.Value = value.ToString("dd.MM.yyyy");
		}

		public static void SetChildNode(XmlNode node, string childNodeName, string value)
		{
			XmlNode child = node.SelectSingleNode(childNodeName);

			if (child == null)
			{
				child = node.OwnerDocument.CreateElement(childNodeName);
				node.AppendChild(child);
			}
			child.InnerXml = "<![CDATA[" + value + "]]>";
		}
	}
}
